<?php
namespace app\examScore\controller;
use app\common\Base;
use app\common\Excel;
use think\Db;
//--优异奖
class ExamYouxiuPrize extends Base {
	
	public function index() {
		$fields = "班级,学号,姓名,语文,语文名次,数学,数学名次,英语,英语名次,物理,物理名次,政治,政治名次,历史,历史名次,政史,政史名次,体育,体育名次,总分,班排,级排";
		$list = explode(",", $fields);
		$this->assign('list',$list);
		return $this->fetch();
	}

	public function pageQuery() {
		$examId = input("examId");
		if(!$examId) return [];
		$grade = input("grade");
		if($grade) {
			$array = explode("_", $grade);
			$grade = $array[0];
		}
		$clazz = input("clazz");
		$gradeId = intval(input("gradeId"));
		$page = input("page");
		$rows = input("rows");
		$sort = input("sort");
		$order = input("order");
		if($clazz) $clazz = $gradeId*100 + intval($clazz);
		$wp = "";
		if($gradeId > 1) {
			$where["年级号"] = $gradeId;
			$syban = Db::table("实验班级")->field("实验班级")->where($where)->find();
			$gjban = Db::table("实验班级")->field("国际班")->where($where)->find();
			$syban = findResult($syban)["实验班级"];
			$gjban = findResult($gjban)["国际班"];
			if($gradeId == 2) {
				$wp = " and ((convert(int,right(班级,2)) not in (".$syban.") and 级排 between 289 and 500) or (convert(int,right(班级,2)) in (".$gjban.") and 级排 between 289 and 880))";
			}else if($gradeId == 3) {
				$wp = " and 级排<=(select max(级排) from 成绩".$grade." where ID in (select top 100 ID from 成绩".$grade." where 考试ID=".$grade." and convert(int,right(班级,2)) not in (".$syban.") and 级排 is not null order by 级排 asc))";
			}
		}
		$result = Db::table("成绩".$grade)
				  ->field("班级,学号,姓名,语文,语文名次,数学,数学名次,英语,英语名次,物理,物理名次,政治,政治名次,历史,历史名次,政史,政史名次,体育,体育名次,总分,班排,级排")
				  ->where("考试ID=".$examId.$wp.($clazz ? " and 班级=".$clazz:""))
				  ->order($sort ? $sort." ".$order : "班级,学号")
				  ->page($page, $rows)
				  ->select();
		$count = Db::table("成绩".$grade)
				  ->where("考试ID=".$examId.$wp.($clazz ? " and 班级=".$clazz:""))
				  ->count();
		$grid["rows"] = selectResult($result);
		$grid["total"] = $count;
		return $grid;
	}

	public function exportExcel() {
		$examId = input("examId");
		if(!$examId) return [];
		$gradeId = intval(input("gradeId"));
		$grade = input("grade");
		if($grade) {
			$array = explode("_", $grade);
			$grade = $array[0];
		}
		$clazz = input("clazz");
		$sort = input("sort");
		$order = input("order");
		if($clazz) $clazz = $gradeId*100 + intval($clazz);
		$syban=Db::query("select 实验班级 from 实验班级 where 年级号=".$gradeId);
		$gjban=Db::query("select 国际班 from 实验班级 where 年级号=".$gradeId);
		$wp = "";
		if($gradeId == 2) {
			$wp = " and ((convert(int,right(班级,2)) not in (".$syban.") and 级排 between 289 and 500) or (convert(int,right(班级,2)) in (".$gjban.") and 级排 between 289 and 880))";
		}else if($gradeId == 3) {
			$wp = " and 级排<=(select max(级排) from 成绩".$grade." where ID in (select top 100 ID from 成绩".$grade." where 考试ID=".$grade." and convert(int,right(班级,2)) not in (".$syban.") and 级排 is not null order by 级排 asc))";
		}
		$result = Db::table("成绩".$grade)
				  ->field("班级,学号,姓名,语文,语文名次,数学,数学名次,英语,英语名次,物理,物理名次,政治,政治名次,历史,历史名次,政史,政史名次,体育,体育名次,总分,班排,级排")
				  ->where("考试ID=".$examId.$wp.($clazz ? " and 班级=".$clazz:""))
				  ->order($sort ? $sort." ".$order : "班级,学号")
				  ->select();
		$result = selectResult($result);
		//excel表头内容
		$fields = "班级,学号,姓名,语文,语文名次,数学,数学名次,英语,英语名次,物理,物理名次,政治,政治名次,历史,历史名次,政史,政史名次,体育,体育名次,总分,班排,级排";
		$list = explode(",", $fields);
		$header = [];
		foreach ($list as $key => $value){
			$header[$value] = $value;
		}
		Excel::toExcel($result,'优秀奖',$header);
		exit;
	}

}
